package cn.edu.shou.lab.homework.work1.jcc.commonMethod;

import cn.edu.shou.lab.homework.work1.jcc.commonClass.Classes;
import cn.edu.shou.lab.homework.work1.jcc.commonClass.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class ComMethod {

    //初始化文件中map对象方法
    public static void init(File file) {
        try {
            if (!file.exists()) {
                file.createNewFile();
                //文件为空,则初始化文件,写入map对象
                //班级是确定的,假设有五个班级,班级的id分别为 1,2,3,4,5
                Classes c1 = new Classes(1);
                Classes c2 = new Classes(2);
                Classes c3 = new Classes(3);
                Classes c4 = new Classes(4);
                Classes c5 = new Classes(5);
                //同时为各个班级创建学生列表
                ArrayList<Student> l1 = new ArrayList<>();
                ArrayList<Student> l2 = new ArrayList<>();
                ArrayList<Student> l3 = new ArrayList<>();
                ArrayList<Student> l4 = new ArrayList<>();
                ArrayList<Student> l5 = new ArrayList<>();
                HashMap<Classes, ArrayList<Student>> map = new HashMap<>();
                map.put(c1, l1);
                map.put(c2, l2);
                map.put(c3, l3);
                map.put(c4, l4);
                map.put(c5, l5);
                //将map写入文件
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(map);
                oos.close();//关闭流
            } else {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //选择 登陆  操作
    public static boolean login(int clsid, int stuid, File file) {
        HashMap<Classes, ArrayList<Student>> map = inputmap(file);
        Set<HashMap.Entry<Classes, ArrayList<Student>>> set = map.entrySet();
        for (HashMap.Entry<Classes, ArrayList<Student>> entry : set) {
            if (entry.getKey().getClsid() == clsid) {
                ArrayList<Student> list = entry.getValue();
                for (Student s : list) {
                    if (s.getStuid() == stuid) {
                        System.out.println(s.getName() + "同学,你已经成功登陆!");
                        return true;
                    }
                }
            }
        }
        System.out.println("对不起,你输入的信息有误,请重新输入或者注册");
        return false;
    }

    //选择 注册(添加学生) 操作
    //返回的是一个学生的id
    public static int addStudent(int clsid, String name, String sex, int age, File file) {
        HashMap<Classes, ArrayList<Student>> map = inputmap(file);
        int id = -1;
        map.forEach((k, v) -> {
            if (k.getClsid() == clsid) {//找到对应的班级
                Student s = new Student(name, sex, age);
                s.setStuid(k.getCreate_stuid() + 1);
                v.add(s);
                k.setCreate_stuid(k.getCreate_stuid() + 1);//学生学号生成器更新
                k.setTotal(k.getTotal() + 1);//学生总数加1
            }
        });
        Set<HashMap.Entry<Classes, ArrayList<Student>>> set = map.entrySet();
        for (HashMap.Entry<Classes, ArrayList<Student>> entry : set) {
            if (entry.getKey().getClsid() == clsid) id = entry.getKey().getCreate_stuid();
        }
        //此时map已经更新,将map写入文件中
        outputmap(map, file);
        return id;
    }

    //选择 查看一个班级的所有学生 操作
    public static void listClassStudent(int clsid, File file) {
        //在控制台打印指定班级中的所有学生
        HashMap<Classes, ArrayList<Student>> map = inputmap(file);
        map.forEach((k, v) -> {
            if (k.getClsid() == clsid) {
                System.out.println("三年 " + clsid + " 班的 " + k.getTotal() + " 位学生信息如下:");
                System.out.println("学号  姓名  性别  年龄");
                for (Student s : v)
                    System.out.println(" " + s.getStuid() + "    " + s.getName() + "   " + s.getSex() + "    " + s.getAge());
            }
        });

    }

    //注销学生 操作   返回是否删除成功
    public static boolean deleteStudent(int clsid, int stuid, File file) {
        HashMap<Classes, ArrayList<Student>> map = inputmap(file);
        Set<HashMap.Entry<Classes, ArrayList<Student>>> set = map.entrySet();
        for (HashMap.Entry<Classes, ArrayList<Student>> entry : set) {
            if (entry.getKey().getClsid() == clsid) {
                ArrayList<Student> list = entry.getValue();
                int flag = 0;//记录是否执行了删除
                for (int i = 0; i < list.size(); ++i) {
                    if (list.get(i).getStuid() == stuid) {
                        list.remove(i);
                        flag = 1;
                        break;
                    }
                }
                //如果执行了删除
                if (flag == 1) {
                    Classes cs = entry.getKey();
                    cs.setTotal(cs.getTotal() - 1);
                    map.replace(cs, list);
                    outputmap(map, file);
                    return true;
                }
            }
        }
        return false;
    }

    //修改学生 操作
    public static void updateInfo(int clsid, int stuid, String optStr,int opt, File file) {
        if(opt <1) return;
        String name = "";
        String sex="";
        int age = 0;
        HashMap<Classes, ArrayList<Student>> map = inputmap(file);
        Set<HashMap.Entry<Classes, ArrayList<Student>>> set = map.entrySet();
        if(opt == 1) name = optStr;
        else if(opt == 2) sex = optStr;
        else age = new Integer(optStr);
        switch (opt){
            case 1:
                for (HashMap.Entry<Classes, ArrayList<Student>> entry : set) {
                    if (entry.getKey().getClsid() == clsid) {
                        ArrayList<Student> list = entry.getValue();
                        for (int i = 0; i < list.size(); ++i) {
                            if (list.get(i).getStuid() == stuid) {
                                list.get(i).setName(name);
                                break;
                            }
                        }
                        Classes cs = entry.getKey();
                        map.replace(cs, list);
                        outputmap(map, file);
                        System.out.println("修改信息成功.");
                        break;
                    }
                }
                break;
            case 2:
                for (HashMap.Entry<Classes, ArrayList<Student>> entry : set) {
                    if (entry.getKey().getClsid() == clsid) {
                        ArrayList<Student> list = entry.getValue();
                        for (int i = 0; i < list.size(); ++i) {
                            if (list.get(i).getStuid() == stuid) {
                                list.get(i).setSex(sex);
                                break;
                            }
                        }
                        Classes cs = entry.getKey();
                        map.replace(cs, list);
                        outputmap(map, file);
                        System.out.println("修改信息成功.");
                        break;
                    }
                }
                break;
            case 3:
                for (HashMap.Entry<Classes, ArrayList<Student>> entry : set) {
                    if (entry.getKey().getClsid() == clsid) {
                        ArrayList<Student> list = entry.getValue();
                        for (int i = 0; i < list.size(); ++i) {
                            if (list.get(i).getStuid() == stuid) {
                                list.get(i).setAge(age);
                                break;
                            }
                        }
                        Classes cs = entry.getKey();
                        map.replace(cs, list);
                        outputmap(map, file);
                        System.out.println("修改信息成功.");
                        break;
                    }
                }
                break;
        }
    }

    //将文件中的map对象读入内存中,以便对其操作,返回值是一个map
    public static HashMap<Classes, ArrayList<Student>> inputmap(File file) {
        HashMap<Classes, ArrayList<Student>> map = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Object o = ois.readObject();
            ois.close();
            if (o instanceof HashMap) {
                map = (HashMap<Classes, ArrayList<Student>>) o;
            } else {
                System.out.println("读取文件中对象失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //将内存中的map对象写入文件中
    public static void outputmap(HashMap<Classes, ArrayList<Student>> map, File file) {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            oos.close();//关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
